package com.nwpu.coursesupport.controller;

import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.exception.TransactionException;
import com.nwpu.coursesupport.service.TaskAssignmentService;
import com.nwpu.coursesupport.service.param.IDListParam;
import com.nwpu.coursesupport.service.param.IDParam;
import com.nwpu.coursesupport.service.param.TaskAssignmentServiceParam;
import com.nwpu.coursesupport.tool.JSONUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/taskAssignment")
public class TaskAssignmentController extends AbstractController {

	@Autowired
	private TaskAssignmentService taskAssignmentService;

	@RequestMapping("/assignLesson")
	@ResponseBody
	public JSONResult getUnAssignLesson(@RequestParam("data")String data){
		JSONResult rst = new JSONResult();
		rst.fail();
		TaskAssignmentServiceParam.UnAssignLessonParam p = JSONUtils.json2pojo(data,
				TaskAssignmentServiceParam.UnAssignLessonParam.class);
		if (p == null) {
			return rst.fail("参数转换有误");
		}
		try {
			rst = this.taskAssignmentService.getUnAssignLesson(p);
		} catch (Exception e) {
			logger.error("分派任务失败");
			return rst.fail("后台出错");
		}
		return rst;
	}


	@RequestMapping(value="/ajaxCreate",method=RequestMethod.POST)
	@ResponseBody
	public JSONResult ajaxCreate(@RequestParam("data") String data) {
		JSONResult rst = new JSONResult();
		rst.fail();
		TaskAssignmentServiceParam.CreateParam p = JSONUtils.json2pojo(data,
				TaskAssignmentServiceParam.CreateParam.class);
		if (p == null) {
			return rst.fail("参数转换有误");
		}
		try {
			rst = this.taskAssignmentService.create(p);
		} catch (Exception e) {
			logger.error("分派任务失败");
			return rst.fail("后台出错");
		}
		return rst;
	}

	@RequestMapping(value="/ajaxBatchCreate",method=RequestMethod.POST)
	@ResponseBody
	public JSONResult batchCreate(@RequestParam("data") String data) {
		JSONResult rst = new JSONResult();
		System.out.println("====="+data);
		rst.fail();
		TaskAssignmentServiceParam.BatchCreateParam p = JSONUtils.json2pojo(data,
				TaskAssignmentServiceParam.BatchCreateParam.class);

//		data="{\"taskId\":1030,\"items\":[{\"lessonId\":1004,\"isVisible\":\"NO\",\"deadline\":\"2020-08-18 00:42\",\"publishDatetime\":\"2020-08-18 20:48\"}]}";
//		TaskAssignmentServiceParam.BatchCreateParam p = JSON.parseObject(data, TaskAssignmentServiceParam.BatchCreateParam.class);
		System.out.println("[ppppp]:"+p);
		if (p == null) {
			return rst.fail("参数转换有误");
		}
		try {
			rst = this.taskAssignmentService.batchCreate(p);
		} catch (TransactionException e) {
			logger.error("批量分派任务失败");
			return rst.fail("分配任务出错！请检查输入信息是否有误，并重新尝试！");
		}
		return rst;
	}

	@RequestMapping(value="/ajaxUpdateAnswer",method=RequestMethod.POST)
	@ResponseBody
	public JSONResult ajaxUpdateAnswer(@RequestParam("data") String data) {
		JSONResult rst = new JSONResult();
		rst.fail();
		TaskAssignmentServiceParam.ShowAnswerParam p = JSONUtils.json2pojo(data,
				TaskAssignmentServiceParam.ShowAnswerParam.class);
		if (p == null) {
			return rst.fail("参数转换有误");
		}
		try {
			rst = this.taskAssignmentService.showAnwserVisible(p);
		} catch (Exception e) {
			logger.error("修改分派记录失败");
			return rst.fail("后台出错");
		}
		return rst;
	}

	@RequestMapping(value="/ajaxUpdateVisible",method=RequestMethod.POST)
	@ResponseBody
	public JSONResult ajaxUpdateVisible(@RequestParam("data") String data) {
		JSONResult rst = new JSONResult();
		rst.fail();
		TaskAssignmentServiceParam.ShowAnswerParam p = JSONUtils.json2pojo(data,
				TaskAssignmentServiceParam.ShowAnswerParam.class);
		if (p == null) {
			return rst.fail("参数转换有误");
		}
		try {
			rst = this.taskAssignmentService.showVisible(p);
		} catch (Exception e) {
			logger.error("修改分派记录失败");
			return rst.fail("后台出错");
		}
		return rst;
	}

	@RequestMapping(value="/ajaxUpdateAssignStatus",method=RequestMethod.POST)
	@ResponseBody
	public JSONResult ajaxUpdateAssignStatus(@RequestParam("data") String data) {
		System.out.println("==="+data);
		JSONResult rst = new JSONResult();
		rst.fail();
		TaskAssignmentServiceParam.UpdateAssignStatusParam p = JSONUtils.json2pojo(data,
				TaskAssignmentServiceParam.UpdateAssignStatusParam.class);
		if (p == null) {
			return rst.fail("参数转换有误");
		}
		try {
			rst = this.taskAssignmentService.updateAssignStatus(p);
		} catch (Exception e) {
			logger.error("修改失败");
			return rst.fail("后台出错");
		}
		return rst;
	}

	@RequestMapping(value="/ajaxUpdateDeadline",method=RequestMethod.POST)
	@ResponseBody
	public JSONResult ajaxUpdateDeadline(@RequestParam("data") String data) {
		JSONResult rst = new JSONResult();
		rst.fail();
		TaskAssignmentServiceParam.UpdateDeadLine p = JSONUtils.json2pojo(data,
				TaskAssignmentServiceParam.UpdateDeadLine.class);
		if (p == null) {
			return rst.fail("参数转换有误");
		}
		try {
			rst = this.taskAssignmentService.updateDeadLine(p);
		} catch (Exception e) {
			logger.error("修改deadLine失败");
			return rst.fail("后台出错");
		}
		return rst;
	}

	@RequestMapping(value="/ajaxDelete",method=RequestMethod.POST)
	@ResponseBody
	public JSONResult ajaxDelete(@RequestParam("id") Integer id) {
		JSONResult rst = new JSONResult();
		rst.fail();

		IDParam p = new IDParam();
		p.setId(id);
		try {
			rst = this.taskAssignmentService.remove(p);
		} catch (Exception e) {
			logger.error("分派记录删除失败");
			return rst.fail("后台出错");
		}
		return rst;
	}

	@RequestMapping(value="/ajaxDeleteAll",method=RequestMethod.POST)
	@ResponseBody
	public JSONResult ajaxDeleteAll(@RequestParam("data") String data) {
		JSONResult rst = new JSONResult();
		rst.fail();
		IDListParam p = JSONUtils.json2pojo(data, IDListParam.class);
		if (p == null) {
			return rst.fail("参数转换有误");
		}
		try {
			rst = this.taskAssignmentService.removeAll(p);
		} catch (Exception e) {
			logger.error("批量删除分派记录失败");
			return rst.fail("后台出错");
		}
		return rst;
	}
}
